package com.whc.gulimall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whc.common.constant.WareConstant;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.Query;
import com.whc.gulimall.ware.dao.PurchaseDetailDao;
import com.whc.gulimall.ware.entity.PurchaseDetailEntity;
import com.whc.gulimall.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StrUtil.isNotBlank(key)) {
            queryWrapper.and(w -> w.eq("purchase_id", key).or().eq("sku_id", key));
        }

        String status = (String) params.get("status");
        if (StrUtil.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }

        String wareId = (String) params.get("wareId");
        if (StrUtil.isNotBlank(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 根据采购单列表修改采购需求状态
     *
     * @param receivePurchaseIds 采购单列表
     */
    @Override
    public void updateDetailsByReceivePurchaseIds(List<Long> receivePurchaseIds) {
        this.update(new UpdateWrapper<PurchaseDetailEntity>()
                .set("status", WareConstant.PurchaseDetailStatusEnum.BUYING.getCode())
                .eq("status", WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode())
                .in("purchase_id", receivePurchaseIds));
    }

}
